<?php
/**
 * @author 	 	 supfire@sohu.com
 * @copyright		 yctool.com
 * @version		 1.0
 * @name 		 file.action.php
 * @todo 		 站点信息程序处理
 *
 * @final 		 2009-05-24
 */
$userid		= $user->userinfo['userid'];
$uf_type	= isset($_GET['uf_type']) ? $_GET['uf_type'] : 1;
$type		= isset($_GET['type']) ? $_GET['type'] : 3;
$actionPost	= isset($_POST['actionPost']) ? $_POST['actionPost'] : "";
$actiondel	= isset($_GET['actiondel']) ? $_GET['actiondel'] : '';
$infoid		= isset($_GET['infoid']) ? $_GET['infoid'] : 0;
$parentid	= isset($_GET['parentid']) ? $_GET['parentid'] : 0;
$fileid		= isset($_GET['fileid']) ? $_GET['fileid'] : 0;
$id		= isset($_GET['id']) ? $_GET['id'] : 0;
$uf_class	= isset($_GET['uf_class']) ? $_GET['uf_class'] : 1;

if($actiondel == 'del' && $id)
{
    $rs = getFileOne($id);
    @unlink('../'.$rs['uf_file']);
    delFile($id);
    //func::showMsg("删除成功", '../', 'admin.php?action=file&uf_type='.$uf_type.'&uf_class='.$uf_class.'&infoid='.$infoid.'&parentid='.$parentid);
}
switch ($uf_class)
{
case 1:
    $typename = '图片';
    break;
case 2:
    $typename = '视频';
 break;
case 3:
    $typename = '附件';
    break;
default:;

}
if($actionPost == 'add')
{
    $field = substr(preg_replace('/\d\.(\d+)\s(\d+)/',   '\\2\\1',   microtime()) , 0 ,16);
    $uf_file = isset($_POST['uf_file']) ? $_POST['uf_file'] : "";
    $filename = isset($_POST['filename']) ? $_POST['filename'] : "";
    $img = $_FILES["uf_file"];

    $pathtemp = Func::getFilePath2($infoid, '../userdata/info/'.$type.'/');
    $path = str_replace('../', '', $pathtemp);

    if($filename)
    {
	foreach ($filename as $key => $value)
	{
	    if($img["size"][$key])
	    {
		if ($img["size"][$key] > 2000000)
		{
		    func::showMsg("文件太大了，不能超过2M", '../');
		}
		if ($uf_type == 1)
		{
		    if (!in_array($img["type"][$key], array("image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png")))
		    {
			$nt = strrchr($img["name"][$key] , ".");
			func::showMsg($nt . " 您上传的图像文件不属于GIF、JPG、PNG中的任何一种", '../');
		    }
		}
	    }
	}
	unset($rsfile);
	foreach ($filename as $key => $value)
	{
	    if($uf_class == 2)
	    {
		$rsfile[$key]["uf_file"] = $uf_file[$key];
		$rsfile[$key]["uf_addtime"] = NOWTIME;
		$rsfile[$key]["infoid"] = $infoid;
		$rsfile[$key]["parentid"] = $parentid;
		$rsfile[$key]["uf_type"] = $uf_type;
		$rsfile[$key]["uf_class"] = $uf_class;
		$rsfile[$key]["filename"] = $value;
	    }
	    elseif($img["size"][$key])
	    {
		if ($uf_type == 1)
		{
		    $nt = strrchr($img["name"][$key] , ".");
		    func::imageResize($img["tmp_name"][$key], 935, 1600, $pathtemp.$field.$key.$nt);
		}
		else
		{
		    move_uploaded_file($img["tmp_name"][$key], $pathtemp.$field.$key.$nt);
		}
		$rsfile[$key]["uf_file"] = $path.$field.$key.$nt;
		$rsfile[$key]["uf_addtime"] = NOWTIME;
		$rsfile[$key]["infoid"] = $infoid;
		$rsfile[$key]["parentid"] = $parentid;
		$rsfile[$key]["uf_type"] = $uf_type;
		$rsfile[$key]["uf_class"] = $uf_class;
		$rsfile[$key]["filename"] = $value;
	    }
	}
    }

    !$rsfile && func::showMsg('没有提交文件', '../');
    unset($img);
    addFile($rsfile);
    func::showMsg('提交成功', '../', 'admin.php?action=file&uf_type='.$uf_type.'&uf_class='.$uf_class.'&infoid='.$infoid.'&parentid='.$parentid);
}
elseif($actionPost == 'update')
{
    !$fileid && func::showMsg('提交文件参数错误', '../');
    $filename = isset($_POST['filename']) ? $_POST['filename'] : "";
    $uf_file = isset($_POST['uf_file']) ? $_POST['uf_file'] : "";
    $img = $_FILES["uf_file"];

    if($filename)
    {
	foreach ($filename as $key => $value)
	{
	    if($img["size"][$key])
	    {
		if ($img["size"][$key] > 2000000)
		{
		    func::showMsg("文件太大了，不能超过2M", '../');
		}
		if ($uf_type == 1)
		{
		    if (!in_array($img["type"][$key], array("image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png")))
		    {
			$nt = strrchr($img["name"][$key] , ".");
			func::showMsg($nt . " 您上传的图像文件不属于GIF、JPG、PNG中的任何一种", '../');
		    }
		}
	    }
	}
	foreach ($filename as $key => $value)
	{
	    if($img["size"][$key])
	    {
		if ($uf_type == 1)
		{
		    $nt = strrchr($img["name"][$key] , ".");
		    func::imageResize($img["tmp_name"][$key], 935, 1600, '../'.$_POST['uf_fileold']);
		}
		else
		{
		    move_uploaded_file($img["tmp_name"][$key], '../'.$_POST['uf_fileold']);
		}
		//$rsfile["uf_file"] = $_POST['uf_fileold'];
		//$rsfile["uf_type"] = $uf_type;
	    }
	    if($uf_class == 2)
	    {
		$rsfile["uf_file"] = $uf_file[$key];		
	    }
	    $rsfile["filename"] = $value;
	}
    }
    (!$rsfile && !$filename) && func::showMsg('没有提交文件', '../');
    unset($img);
    updateFile($rsfile, 'fileid = '.$fileid);
    func::showMsg('提交成功', '../', 'admin.php?action=file&uf_type='.$uf_type.'&fileid='.$fileid.'&uf_class='.$uf_class.'&infoid='.$infoid.'&parentid='.$parentid);
}elseif($fileid)
{
    $rsOne = getFileOne($fileid);
}elseif(!$actionPost)
{
    $rs = getFile($infoid, $uf_type, $uf_class, $parentid);
}
//uf_addtime uf_updatetime filename uf_file uf_uid fileid
//处理数据库的函数：
//begin
/**
 * 添加文件
 *
 */
function addFile($arrayvalue)
{
    return db::insertLot(DB_TABLEPRE . 'web_file', $arrayvalue);
}

/**
 * 修改文件
 *
 */
function updateFile($arrayvalue, $whereSql)
{
    return db::update(DB_TABLEPRE . 'web_file', $arrayvalue, $whereSql);
}

/**
 * 删除文件
 *
 */
function delFile($fileid)
{
    //return db::updateRs(DB_TABLEPRE . 'web_file', "uf_flag = '-1'", 'fileid = '.$fileid);
    return db::deleteRs(DB_TABLEPRE . 'web_file', 'fileid = '.$fileid);
}

/**
 * 通过$userid $uf_type获取文件的信息  
 * @param int $userid $uf_type
 * @return array $rs
 * @author supfire
 * @final 2008-08-05
 */
function getFile($infoid, $uf_type, $uf_class, $parentid = 0)
{
    return db::getAll(DB_TABLEPRE . 'web_file', '*', "uf_type = '$uf_type' AND 
	uf_class = '$uf_class' AND infoid = '$infoid' AND parentid = '$parentid' AND uf_flag <> '-1'");		
}
/**
 * 通过$fileid获取文件的信息  
 * @param int $fileid
 * @return array $rs
 * @author supfire
 * @final 2008-08-05
 */
function getFileOne($fileid)
{
    return db::getRow(DB_TABLEPRE . 'web_file', '*', "fileid = '$fileid'");		
}
//end
//处理数据库的函数：
?>
